<template>
  <div class="login">
    <van-nav-bar>
      <template #left>
        <div class="left-box">
          <div class="logo">
            <img class="auto-img" src="../assets/home_active.png" alt="" />
          </div>
          <div class="logo-title">Luckin Coffee</div>
        </div>
      </template>

      <template #right>
        <div class="right-box" @click="goMenu">先逛一逛</div>
      </template>
    </van-nav-bar>
    <div class="login-box">
      <div class="login-text">
        <div class="welcome">欢迎回来！</div>
        <div class="welcome-text">Please login to your accounts</div>
      </div>

      <div class="form-box">
        <van-form @submit="login">
          <van-field
            v-model="user.phone"
            name="手机号"
            label="手机号"
            placeholder="手机号"
            autocomplete="off"
            :rules="[{ required: true, message: '请填写正确的手机号', pattern: phoneReg}]"
          />
          <van-field
            v-model="user.password"
            :type="isShow ? 'text' : 'password'"
            name="密码"
            label="密码"
            placeholder="密码"
            :right-icon="isShow ? 'eye-o' : 'closed-eye'"
            @click-right-icon="showPassword"
            autocomplete="off"
            :rules="[{ required: true, message: '必须以字母开头,6-16位字母数字下划线组合', pattern: passwordReg}]"
          />

          <div class="forgot">
            <div class="forgot-text" @click="toRetrievePassword">忘记密码？</div>
          </div>

          <div class="login-btn">
            <van-button round block color="#0C34BA" native-type="submit"
              >登录</van-button
            >
          </div>
        </van-form>

        <div class="login-btn">
          <van-button round block type="default" @click="showRegiterPopup">注册</van-button>
        </div>
      </div>
    </div>

    <!-- 注册 -->
    <van-popup
      v-model="showPopup"
      position="bottom"
      closeable
    >
      <div class="popup-box">
        <div class="regitser-title">注册</div>
        <div class="register-form-box">
          <van-form @submit="register">
            <van-field
              v-model="info.phone"
              name="手机号"
              label="手机号"
              placeholder="手机号"
              autocomplete="off"
              :rules="[{ required: true, message: '请填写正确的手机号', pattern: phoneReg}]"
            />
            <van-field
              v-model="info.password"
              :type="isShow ? 'text' : 'password'"
              name="密码"
              label="密码"
              placeholder="密码"
              :right-icon="isShow ? 'eye-o' : 'closed-eye'"
              @click-right-icon="showPassword"
              autocomplete="off"
              :rules="[{ required: true, message: '必须以字母开头,6-16位字母数字下划线组合', pattern: passwordReg}]"
            />

            <van-field
              v-model="info.nickName"
              type="text"
              name="昵称"
              label="昵称"
              placeholder="昵称"
              autocomplete="off"
              :rules="[{ required: true, message: '1-16位中英文数字下划线组合', pattern: nickNameReg}]"
            />

            <div class="login-btn">
              <van-button round block color="#0C34BA" native-type="submit"
                >注册</van-button
              >
            </div>
          </van-form>
        </div>
      </div>
    </van-popup>
  </div>
</template>

<script>
export default {
  name: "Login",

  data() {
    return {

      //登录信息
      user: {
        phone: "",
        password: "",
      },

      // 注册信息
      info: {
        phone: '',
        password: '',
        nickName: ''
      },

      //手机号正则
      phoneReg: /^1[3-9]\d{9}$/,

      //密码正则(必须以字母开头, 字母数字_组合[6-16])
      passwordReg: /^[A-Za-z]\w{5,15}$/,

      //昵称正则(中英文数字下划线组合[1-16])
      nickNameReg: /^[\u4e00-\u9fa5A-Za-z0-9_]{1,16}$/,


      //是否明文显示密码
      isShow: false,

      showPopup: false,
    };
  },

  methods: {
    // 忘记密码
    toRetrievePassword(){
      this.$router.push({name:'RetrievePassword'})
    },
    //显示密码
    showPassword() {
      this.isShow = !this.isShow;
    },

    //显示注册弹窗
    showRegiterPopup() {
      this.showPopup = true;
    },

    //注册
    register() {

      this.axios({
        method: 'post',
        url: '/register',
        data: {...this.info}
      }).then(res => {
                        this.$toast(res.data.msg);

        //注册成功
        if (res.data.code === 100) {
          this.showPopup = false;
          //清除注册信息
          for (let key in this.info) {
            this.info[key] = '';
          }
        }

      }).catch(err => {
              })
    },

    //登录
    login() {
      this.axios({
        method: 'post',
        url: '/login',
        data: {
          ...this.user
        }
      }).then(res => {
        
        this.$toast(res.data.msg);

        if (res.data.code === 200) {
          //登录成功

          //将token保存到sessionStorage
          sessionStorage.setItem('token', res.data.token);

          setTimeout(() => {
            this.$router.push({name: 'Menu'});
          }, 2000)
        }
      }).catch(err => {
              })
    },

    goMenu() {
      this.$router.push({name: 'Menu'});
    }
  },
};
</script>

<style lang="less" scoped>
.login {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background-color: #fff;

  .register-form-box{
    margin-top: 30px;
  }

  .regitser-title {
    font-size: 16px;
  }

  .popup-box {
    padding: 15px;
  }

  .forgot {
    display: flex;
    margin-top: 20px;
  }

  .forgot-text {
    margin-left: auto;
    font-size: 14px;
    color: #666;
  }

  .login-btn {
    margin-top: 50px;
  }

  .form-box {
    margin-top: 50px;
  }

  /deep/ .van-cell {
    padding: 10px 0;
  }

  .welcome {
    font-size: 22px;
    font-weight: bold;
  }

  .welcome-text {
    color: #666;
    margin-top: 20px;
    font-size: 16px;
  }

  .login-box {
    padding: 20px;
  }

  .login-text {
    margin-top: 50px;
  }

  .right-box {
    font-weight: bold;
    font-size: 14px;
    color: #0c34ba;
  }

  .left-box {
    display: flex;
    align-items: center;
  }

  .logo {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    margin-right: 10px;
  }

  .logo-title {
    font-weight: bold;
    color: #444;
    font-size: 14px;
  }
}
</style>